package com.ruoyi.psi.sale.salereturn.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.sale.salereturn.domain.SaleReturn;
import com.ruoyi.psi.sale.salereturn.domain.SaleReturnList;
import com.ruoyi.psi.sale.salereturn.service.ISaleReturnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售订单退货
 * 修改：lsy 2021.7.17
 */
@RestController
@RequestMapping("/psi/sale/return")
@Api(value = "销售订单退货信息模块", tags = "销售订单退货接口")
public class SaleReturnController  extends BaseController {
    @Autowired
    private ISaleReturnService saleReturnService;


    /**
     * 获取销售订单退货
     * @param saleReturn
     * @return
     */
    @ApiOperationSupport(includeParameters = {"SaleReturn.saleReturnId", "SaleReturn.warehouseId", "SaleReturn.customerInfoId", "SaleReturn.returnTime"})
    @ApiOperation(value = "获取获取销售订单退货列表",notes = "查询所有获取销售订单退货信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SaleReturn saleReturn) {
        startPage();

        List<SaleReturn> saleReturns = saleReturnService.selectSaleReturnList(saleReturn);
        return getDataTable(saleReturns);
    }

    /**
     * 导出销售订单退货
     *
     * @param saleReturn
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SaleReturn saleReturn) {
        List<SaleReturn> saleReturns = saleReturnService.selectSaleReturnList(saleReturn);
        ExcelUtil<SaleReturn> util = new ExcelUtil<SaleReturn>(SaleReturn.class);
        return util.exportExcel(saleReturns, "销售订单退货单数据");
    }

    /**
     * 新增销售订单退货时，获取对应的销售出库详细
     *
     * @param saleReturn
     * @return
     */
    @PostMapping("/getSaleDeliveryListForReturn")
    public AjaxResult getSaleDeliveryListForReturn(@RequestBody SaleReturn saleReturn) {
        List<SaleReturnList> saleReturnLists = saleReturnService.getSaleDeliveryListForReturn(saleReturn.getSaleDeliveryId());

        return AjaxResult.success(saleReturnLists);
    }
    /**
     * 新增销售订单退货
     * @param saleReturn 销售订单退货单据
     * @return
     */
    @ApiOperation(value = "新增销售订单退货", notes = "新增销售订单退货")
    @ApiImplicitParam(name = "saleReturn", value = "新增销售订单退货(参数：saleReturnId，saleDeliveryId，warehouseId，remark)", dataType = "SaleReturn")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SaleReturn saleReturn) {
        try {
            saleReturnService.insertSaleReturn(saleReturn);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新销售订单退货
     * @param saleReturn 销售订单退货单据
     * @return
     */
    @ApiOperation(value = "更新销售订单退货", notes = "更新销售订单退货")
    @ApiImplicitParam(name = "saleReturn", value = "更新销售订单退货", dataType = "SaleReturn")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SaleReturn saleReturn) {
        try {
            saleReturnService.updateSaleReturn(saleReturn);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除销售订单退货
     * 仅修改删除标志位
     *
     * @param saleReturn 销售订单退货单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SaleReturn saleReturn) {

        return toAjax(saleReturnService.updateSaleReturnDeleteFlag(saleReturn));
    }

}
